/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Description: Hmfs-tools mkfs check point writer
 */

#ifndef CHECKPOINT_WRITER_H
#define CHECKPOINT_WRITER_H

#include <memory>
#include <string>
#include <sys/types.h>

#include "hmfs_command.h"
#include "hmfs_fs.h"

namespace OHOS {
namespace Hmfs {
union CheckPointOnDisk {
    CheckPointData cp;
    uint8_t size[HMFS_BLOCK_SIZE];
};

class MkfsFormat;
class CheckPointWriter {
public:
    CheckPointWriter(MkfsFormat &mkfs);
    ~CheckPointWriter() = default;

    int32_t Format();

private:
    MkfsFormat &mkfs_;
    std::unique_ptr<CheckPointOnDisk> checkPoint_ = nullptr;
    std::unique_ptr<uint8_t[]> activeSegments_ = nullptr;
    std::unique_ptr<SummaryBlockData> hotNodeSumary_ = nullptr;
    std::unique_ptr<SummaryBlockData> warmColdNodeSumary_ = nullptr;
    std::unique_ptr<uint8_t[]> natBits_ = nullptr;
    std::unique_ptr<uint8_t[]> emptyBlock_ = nullptr;
    uint64_t writeBlockId_ = 0;
    uint32_t natBitmapSize_ = 0;
    uint32_t natBitsAreablocks_ = 0;

    int32_t PrepareCheckPointData();
    int32_t PrepareCheckPointDataEnd(CheckPointData *checkPoint);
    void MakeCheckPointCrc();
    int32_t PrepareActiveSegments();
    int32_t FillNatJournal(JournalEntry *natJournal);
    void FillSitJournal(JournalEntry *sitJournal);
    void FillSitJournalSegData(JournalEntry *sitJournal, uint32_t entryIndex);
    int32_t PrepareNodeSummary();
    int32_t PrepareNatBit();
    int32_t PrepareEmptyBlock();
    int32_t WriteCpStruct();
    int32_t WriteCpPayload();
    int32_t WriteActiveSegments();
    int32_t WriteNatBit();
};

} // namespace Hmfs
} // namespace OHOS
#endif // CHECKPOINT_WRITER_H
